package com.starblink.library.widget.search

import android.content.Context
import android.util.AttributeSet
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.ext.inflater
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.extension.navigation
import com.starblink.basic.route.RoutePage
import com.starblink.basic.style.view.recyclerview.SimpleGapItemDecor
import com.starblink.library.widget.databinding.CellSearchComposedWordBinding
import com.starblink.library.widget.databinding.WidgetSearchWordsShownBinding
import com.starblink.library.widget.search.cell.SearchComposedWordCell

class SearchWordShownView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    styleTheme: Int = 0
) : ConstraintLayout(context, attrs, styleTheme) {

    val binding = WidgetSearchWordsShownBinding.inflate(inflater, this, true)

    private val composedWordList = arrayListOf<String>()

    private val composedWordAdapter by lazyOnNone {
        MutableAdapter(composedWordList).apply {
            addVhDelegate {
                SearchComposedWordCell(
                    CellSearchComposedWordBinding.inflate(
                        it.inflater,
                        it,
                        false
                    )
                ) {
                    composeWordClick?.invoke(it)
                }
            }
        }
    }

    var onCameraIconTap: (() -> Unit)? = null
    var onCloseIconTap: (() -> Unit)? = null

    var keyword: String? = null
        set(value) {
            field = value
            binding.tvWord.text = value
        }

    var composeWords: List<String>? = null
        set(value) {
            field = value
            binding.recyclerCompose.visibleOrGone(field?.isNotEmpty() == true) {
                field?.takeIf { it.isNotEmpty() }?.let {
                    composedWordList.clear()
                    composedWordList.addAll(it)
                    composedWordAdapter.refreshAll()
                    binding.recyclerCompose.scrollToPosition(0)
                }
            }
        }


    var composeWordClick: ((index: Int) -> Unit)? = null

    var hint: CharSequence? = null
       set(value) {
           field = value
           binding.tvWord.hint = value
       }


    init {
        binding.btnCamera.setOnClickListener {
            if (onCameraIconTap != null) {
                onCameraIconTap?.invoke()
            } else {
                navigation(RoutePage.ImageSearch.PAGE_CAMERA)
            }
        }
        binding.ivClose.setOnClickListener {
            if (onCloseIconTap != null) {
                onCloseIconTap?.invoke()
            }
        }

        binding.recyclerCompose.layoutManager =
            LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        binding.recyclerCompose.adapter = composedWordAdapter
        binding.recyclerCompose.addItemDecoration(SimpleGapItemDecor(withEdge = true, horGap = 4.dp2px()))

    }
}